<?php
require_once(dirname(__FILE__)."/inc/session_auth.php");
    require_once(dirname(__FILE__).'/inc/config.php');
    require_once(dirname(__FILE__).'/inc/debug.php');
    require_once(dirname(__FILE__).'/lib/Savant/Savant3.php');
    
    /*
     * Session Management (check authentication status)
     */
    require_once(dirname(__FILE__).'/inc/class/user/User.php');
    // check user state
    $wfuser = new WFUser();
    if( !$wfuser->isFailed() ) {
        mydebug_log("[INDEX] WFUSER created");
        if( STANDALONE_WF ) {
            mydebug_log("[INDEX] trying STANDALONE recover");
            // we don't care is it recovered.
            $wfuser->standalone_user_init();
        } else {
            mydebug_log("[INDEX] trying PRODUCT recover");
            if( false == $wfuser->product_user_init() ) {
                echo "<script>alert('Widget Framework Error: ".$wfuser->getErrMessage()."');</script>";
                exit;
            }
            mydebug_log("[INDEX] trying PRODUCT recover - ok");
        }
    }
    // make 'check' result
    $ckresult = $wfuser->check_result(0,1);

    // we won't update $_SESSION.
    session_write_close();
    $PageName = $_GET['p'];
    $assoc = WF::getControllerFactory()->getDbController()->getAssocPageInfoByCondition($PageName,$_SESSION['uid']);
    $CID = $assoc['container_id'];
    $CNA = $assoc['container_name'];
    $CTN = $assoc['container_type_name'];
    $PID = $assoc['page_number'];
	//cookie:  set the container ID 
	setcookie('cname', $CNA , time()+ 3600, WF_COOKIE_PATH);
    if(isset($PageName))
	{
        if($PID!=$_COOKIE["lastTab"] && $CNA == "dashBoard"){
            if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']){
                $strQueryString = $_SERVER['QUERY_STRING'];
            }
            $strQueryString = $strQueryString ? '?'.$strQueryString : '?p='.$PageName;
            setcookie('lastTab', $PID , 0, WF_COOKIE_PATH);
            header("Location: index.php".$strQueryString);
        }
    }
    
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title><?php echo $wfconf_sitename ; ?></title>
<script>
    var wfc_standalone = <?php echo (STANDALONE_WF)? 'true':'false'; ?>;
    var homeBase = "<?php echo $wfconf_base; ?>";
    var modFolder = "<?php echo $wfconf_dir_modules; ?>";
    var WP_WEBROOT_NO_SLASH = "<?php echo WP_WEBROOT_NO_SLASH; ?>";
    var UI_DIR = "<?php echo UI_DIR; ?>";
    var USER_GENERATED_WIDGET_DIR = "<?php echo USER_GENERATED_WIDGET_DIR; ?>";     
    var dbHandler="<?php echo $wfconf_handler_db; ?>"; 
    var proxyHandler="<?php echo $wfconf_handler_ProxyRequest; ?>";    
    var wfc_js_compact = <?php echo $wfconf_js_compact ? 'true' : 'false' ; ?>;
	var wfc_widget_compact = <?php echo $wfconf_widget_compact ? 'true' : 'false' ; ?>;	
    var UI = {};
    var server_check_result = '';
    var wfconf_client_debug_max_stack_size ="<?php echo $GLOBALS['wfconf_client_debug_max_stack_size']; ?>";
    var wfconf_client_debug_console ="<?php echo $GLOBALS['wfconf_client_debug_console']; ?>";
    var wfconf_client_debug_level ="<?php echo $GLOBALS['wfconf_client_debug_level']; ?>";
	var wfc_widget_product_logon_page = "<?php echo $wfconf_product_logon_page; ?>";	
    var wfconf_client_debug_console_debugger_userIds = "<?php echo$GLOBALS['wfconf_client_debug_console_debugger_userIds']; ?>".split(",");
    var wfconf_default_menu = "<?php echo $GLOBALS['wfconf_default_menu']; ?>";
    var wfconf_client_debug_window_onerror = "<?php echo $GLOBALS['wfconf_client_debug_window_onerror']; ?>";
    var wf_cookie_path = '<?php echo WF_COOKIE_PATH; ?>';
    var wfconf_use_jquery = '<?php echo $GLOBALS["wfconf_use_jquery"]; ?>';
    var wfcid = <?php echo $CID; ?>;
    var wfcname = '<?php echo $CNA; ?>';
    var wfctype = '<?php echo $CTN; ?>';
    var ajaxTimeoutValue_update_widgetPool = <?php echo ($GLOBALS['wp_max_execution_time']*1000);?>;
<?php
    if( $ckresult != false ) {
        $ckresponse = array();
        $ckresponse['response'] = "OK";
        $ckresponse['result'] = $ckresult;
    }

    $arrUserGeneratedInfoOfWidget = $wfuser->getUserGeneratedInfoOfWidget();
?>
    g_arrUserGeneratedInfoOfWidget = <?php echo json_encode($arrUserGeneratedInfoOfWidget); ?>;
        
    <?php 
    $arrWidgetBaseNameGotL10NFile = WF::getLangFactory()->getL10N()->getArrWidgetBaseNameGotL10NFile();
    $js = '';
    if(is_array($arrWidgetBaseNameGotL10NFile) && $arrWidgetBaseNameGotL10NFile){
    	$js = "'".implode("','",$arrWidgetBaseNameGotL10NFile)."'";
    }
    ?>
    var g_arrWidgetBaseNameNeedToPreloadL10N = [<?php echo $js;?>];
    var wpm_check_new_scm;
    var wpm_check_new = false;
    var wpm_check_widget = false;
    var wpm_check_configure_widget_and_widget_component = false;
    <?php if($GLOBALS['wfconf_scm_and_widget_pool_update'] == true ): ?>
    wpm_check_new_scm = <?php echo json_encode(WF::getWidgetPoolFactory()->getWidgetPoolManager()->isSCMUpdate($_COOKIE)); ?>;
    wpm_check_new = <?php echo WF::getWidgetPoolFactory()->getWidgetPoolManager()->isNewWidget() ? 'true': 'false';?>;        
    wpm_check_widget = wpm_check_new_scm['is_new_widgets_update'];
    wpm_check_configure_widget_and_widget_component = wpm_check_new_scm['is_new_wcu_update'];
    <?php endif; ?>
    //default language
    var defaultLang='<?php echo $wfconf_default_user_lang;?>';
</script>

<?php
    if($wfconf_js_compact){
?>
<link rel="stylesheet" type="text/css" href="<?php echo WP_WEBROOT_NO_SLASH;?>/theme/default/theme-min.css">
<link rel="stylesheet" type="text/css" href="ui/message/style.css">
<link rel="stylesheet" type="text/css" href="ui/modal/style.css">
<?php
    }else{
?>
<link rel="stylesheet" type="text/css" href="common/css/reset-fonts-grids.css">
<link rel="stylesheet" type="text/css" href="<?php echo WP_WEBROOT_NO_SLASH;?>/theme/default/theme.css">
<link rel="stylesheet" type="text/css" href="ui/message/style.css">
<link rel="stylesheet" type="text/css" href="ui/modal/style.css">
<?php
    }  
?>
<link id='style1' rel="stylesheet"> 
</head>
<body onload="WidgetMainPageOnload()" class="grey">
<?php require_once(dirname(__FILE__)."/body.php"); ?>
</body>
<script type="text/javascript" src='<?php echo WP_WEBROOT_NO_SLASH;?>/template/<?php echo $CTN;?>/pageSettings.js'></script>
<?php
if($wfconf_js_compact){ //If $wfconf_js_compact = true
?>
<script type="text/javascript" src='lib/RequireJS/require.js'></script>
<?php
      if ($GLOBALS['wfconf_client_debug_level'] != "OFF"){
?>
<script type="text/javascript" src='js/wf-debug-min.js'></script>
<?php }else{?>
<script type="text/javascript" src='js/wf-min.js'></script>
<?php }?>
<?php
        if ($GLOBALS["wfconf_use_jquery"]){
?>
<script type="text/javascript" src='js/wf-module-jquery-min.js'></script>
<?php
        }else{
?>
<script type="text/javascript" src='js/wf-module-min.js'></script>
<?php
        }
    }else{ //If $wfconf_js_compact = false
?>
<script type="text/javascript" src='lib/RequireJS/require.src.js'></script>
<script type="text/javascript" src='lib/MooTools/mootools-big.js'></script>
<script type="text/javascript" src='lib/MooTools/mootools-compat-big.js'></script>
<?php
if ($GLOBALS['wfconf_client_debug_level'] != "OFF"){
?>
<script type="text/javascript" src='lib/log4javascript/log4javascript.js'></script>
<script type="text/javascript" src='lib/javascript-stacktrace/stacktrace.js'></script>
<script type="text/javascript" src='js/ClientLogger.js'></script>
<?php
}
?>
<script type="text/javascript" src='js/language.js'></script>
<script type="text/javascript" src='ui/message/Message.js'></script>
<script type="text/javascript" src='js/common.js'></script>
<script type="text/javascript" src='ui/window/Window.js'></script>
<script type="text/javascript" src='ui/component/Component.js'></script>
<script type="text/javascript" src='ui/layout/Layout.js'></script>
<script type="text/javascript" src='ui/modal/Modal.js'></script>
<script type="text/javascript" src='ui/table/SortingTable.js'></script>
<script type="text/javascript" src='ui/pager/Pager.js'></script>
<script type="text/javascript" src='js/window.js'></script>    
<script type="text/javascript" src='js/page.js'></script>
<script type="text/javascript" src='js/tm-common.js'></script>
<script type="text/javascript" src='js/tm-serverlist.js'></script>
<script type="text/javascript" src='js/WidgetPackageManager.js'></script>
<script type="text/javascript" src='js/validation.js'></script>    
<script type="text/javascript" src='js/common_validation.js'></script>
<script type="text/javascript" src='js/url_validation.js'></script>
<script type="text/javascript" src='js/UrlProcessor.js'></script>
<script type="text/javascript" src='js/wfrequest.js'></script>
<script type="text/javascript" src='<?php echo UI_DIR; ?>/modLogin/module.js'></script>            
<script type="text/javascript" src='<?php echo UI_DIR; ?>/modThemes/module.js'></script>
<script type="text/javascript" src='<?php echo UI_DIR; ?>/modModules/module.js'></script>
<script type="text/javascript" src='<?php echo UI_DIR; ?>/serverlist/Serverlist.js'></script>
<script type="text/javascript" src='<?php echo UI_DIR; ?>/modModalWidgetSetting/module.js'></script>
<script type="text/javascript" src='<?php echo UI_DIR; ?>/modModalHelpPage/module.js'></script>
<script type="text/javascript" src='js/wf.js'></script>
<script type="text/javascript" src='js/init_wf.js'></script>
<?php
    if ($GLOBALS["wfconf_use_jquery"]){
?>
<script type="text/javascript" src='lib/jQuery/jquery.js'></script>
<?php
    }
}
?>
<script>
    if(wfconf_use_jquery){
        jQuery.noConflict();
    }
</script>
<script type="text/javascript" src='<?php echo WP_WEBROOT_NO_SLASH; ?>/template/<?php echo $CTN;?>/common.js'></script>
<script>
    if (wfconf_client_debug_level!= "OFF"){
        if (wfconf_client_debug_window_onerror){
			window.onerror = function(message, url, line){
				clientLogger.windowError(message, url, line, printStackTrace().join("\n"));
			};
        }
    }
    var themes = new Array();
    if(DisplayPageHeaderMenu){
    var g_serverlist=new TM_ServerList();
    g_serverlist.adjustLastCheckTimedifference();
    }
<?php
    if (isset($_GET['view'])){
        switch ($_GET['view']){
            case 'serverlist'   : $serverlist_conf = '';
                                $tmpArr = array();
                                if (isset($_GET['showservertypecolumn'])){
                                    $tmpStr = strtolower($_GET['showservertypecolumn']);
                                    if ($tmpStr === 'true' || $tmpStr === 'false'){
                                        $tmpArr[] = 'showServerTypeColumn:' . $tmpStr;
                                    }
                                }
                                if (isset($_GET['supportservertypes'])){
                                    $tmpArr[] = "supportServerTypes:['" . str_replace(',',"','",$_GET['supportservertypes']) . "']";
                                }
                                if (count($tmpArr) > 0){
                                    $serverlist_conf = '{' . implode(',',$tmpArr) . '}';
                                }
                                echo 'new WFServerList(' . $serverlist_conf . ')';
                                break;
            default : WF::getLogger()->debug('view:' . $_GET['view'] . ' not supported.');
                    break;
        }
    }
?>

    //alert( Cookie.get('wids') );
    
     var userID = Cookie.get('userID',{path:wf_cookie_path}) ;
    if(userID) {
        var xl1 = l1;
        var xl2 = l2.split(",");
        var xl3 = l3.split(",");
        var xl4 = l4.split(",");
        var xl5 = l5.split(",");
        var xl6 = l6.split(",");
        var xl7 = l7.split(","); 
    }
    window.addEvent('domready', function(){
        initDocument();
    });  
    IncludeAdditionalScript();
    var widgetList;
</script>
</html>
